// hyb_yl/pharmacy//pages/index/index.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    swiper_current: 0,
    list_index: 0,
    screen_state: false,
    scrollState: false,
    page:0,
    footer: {
      footerTab: 3,
      footerlist: [{
        text: "首页",
        img: "/hyb_yl/images/tab01.png",
        selimg: "/hyb_yl/images/tab1.png",
        url: "/hyb_yl/tabBar/index/index"
      }, {
        text: "资讯",
        img: "/hyb_yl/images/tab02.png",
        selimg: "/hyb_yl/images/tab2.png",
        url: "/hyb_yl/tabBar/jibingyufang/jibingyufang"
      }, {
        text: "分享",
        img: "/hyb_yl/images/tab03.png",
        selimg: "/hyb_yl/images/tab3.png",
        url: "/hyb_yl/tabBar/community/community"
      }, {
        text: "药房",
        img: "/hyb_yl/images/tab04.png",
        selimg: "/hyb_yl/images/tab4.png",
        url: "/hyb_yl/tabBar/shop/shop"
      }, {
        text: "问诊",
        img: "/hyb_yl/images/tab04.png",
        selimg: "/hyb_yl/images/tab4.png",
        url: "/hyb_yl/tabBar/fastnavigate/fastnavigate"
      }, {
        text: "我的",
        img: "/hyb_yl/images/tab05.png",
        selimg: "/hyb_yl/images/tab5.png",
        url: "/hyb_yl/tabBar/my/my"
      }]
    },
    condition: [
    //   {
    //   title: '商家服务',
    //   selection: 'multi',
    //   list: [{
    //     state: false,
    //     name: "美团专送"
    //   },
    //   {
    //     state: false,
    //     name: "24h营业"
    //   },
    //   {
    //     state: false,
    //     name: "开发票"
    //   },
    //   ]
    // },
    {
      title: '特色精选',
      selection: 'single',//single 单选 multi多选
      list: []
    },
    // {
    //   title: '优惠活动',
    //   selection: 'multi',
    //   list: [{
    //     state: false,
    //     name: "全店满减"
    //   },
    //   {
    //     state: false,
    //     name: "减配送费"
    //   }
    //   ]
    // }
    ],
    sort_list: [
      {
        title: "综合排序",
      }, {
        title: "评分最高",
      }, {
        title: "距离最近",
      }
    ],
    // screen_li: [
    //   {
    //     title: '支持预定',
    //     state: false
    //   }, {
    //     title: '支持预定',
    //     state: false
    //   }, {
    //     title: '支持预定',
    //     state: false
    //   }, {
    //     title: '支持预定',
    //     state: false
    //   }
    // ],
    list: [],
    type:"",
    keyword:"",
    order:"",
    speed:"",
    sell:"",
    fee:"",
    level:""
  },
  close_screen: function () {
    this.setData({
      screen_state: false
    })
  },
  tab_btn: function (e) {
    var screen_li = this.data.screen_li, index = e.currentTarget.dataset.index;
    screen_li[index].state = !screen_li[index].state
    this.setData({
      screen_li: screen_li
    })
  },
  // 查看药房详情
  todetail:function(e){
    var that = this;
    var hid = e.currentTarget.dataset.hid;
    var agentname = e.currentTarget.dataset.agentname
    wx.navigateTo({
      url: '/hyb_yl/pharmacy/pages/pharmacy_cont/pharmacy_cont?hid='+hid+'&agentname='+agentname,
    })
  },
  condition_btn: function (e) {
    var state = e.currentTarget.dataset.state;
    console.log(e)
    this.setData({
      screen_state: true,
      condition_state: state
    })
  },
  multi_btn: function (e) {
    console.log(e)
    var index = e.currentTarget.dataset.index, oid = e.currentTarget.dataset.oid, that = this, condition = this.data.condition;
    condition[oid].list[index].state = !condition[oid].list[index].state
    this.setData({
      condition: condition
    })
  },
  single_btn: function (e) {
    var index = e.currentTarget.dataset.index, oid = e.currentTarget.dataset.oid, that = this, condition = this.data.condition;
    if (condition[oid].list[index].state) {
      condition[oid].list[index].state = false
    } else {
      for (var i = 0; i < condition[oid].list.length; i++) {
        condition[oid].list[i].state = false
      }
      condition[oid].list[index].state = true
    }
    this.setData({
      condition: condition,
      level:e.currentTarget.dataset.id,
      page:0,
      list:[]
    })
    this.getList();
  },
  swiper_change: function (e) {
    this.setData({
      swiper_current: e.detail.current
    })
  },
  list_bind: function (e) {
    this.setData({
      list_index: e.currentTarget.dataset.index,
      type:e.currentTarget.dataset.id,
      page:0,
      list:[]
    })
    this.getList();
  },
  onPageScroll: function (e) {
    if (e.scrollTop >= 524) {
      this.setData({
        scrollState: true
      })
    } else {
      this.setData({
        scrollState: false
      })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getfooter()
  },
  getfooter() {
    var that = this
    app.util.request({
      url: "entry/wxapp/index.footerlist",
      data: {
        openid: wx.getStorageSync('openid')
      },
      success: function (resing) {
        let footers = {
          footerTab: that.data.index,
          footerlist: resing.data
        };
        that.setData({
          footer: footers
        })
      }
    })
  },
  // 获取类型
  getType: function () {
    var that = this;
    app.util.request({
      url: "entry/wxapp/goods.goods_type",
      success: function (res) {
        console.log(res)
        var type_list = res.data.lists
        var type_lists = res.data.list
        that.setData({
          type_list: type_list,
          type_lists:type_lists
        })
      },
    });
  },
  // 获取医院级别
  getHospital_level:function(){
    var that = this;
    app.util.request({
      'url':"entry/wxapp/goods.Hospital_level",
      success:function(res){
        var list = res.data;
        var condition = that.data.condition;
        condition[0].list = list;
        that.setData({
          condition:condition
        })
      }
    })
  },
  // 获取列表
  getList: function () {
    var that = this;
    wx.getLocation({
      type: 'wgs84',
      success:function(res){
        var longitude = res.longitude
        var latitude = res.latitude
        app.util.request({
          url: "entry/wxapp/goods.shop_list",
          data: {
            longitude:longitude,
            latitude:latitude,
            type: that.data.type,
            keyword: that.data.keyword,
            order: that.data.order,
            speed: that.data.speed,
            sell: that.data.sell,
            fee: that.data.fee,
            page: that.data.page,
            level:that.data.level
          },
          success: function (res) {
            var page = that.data.page + 1;
            var list = res.data
            that.setData({
              page:page,
              list: that.data.list.concat(list)
            })
          }
        })
      }
    })
    

  },
  // 关键词输入
  changekeyword:function(e){
    var keyword = e.detail.value;
    var that = this;
    that.setData({
      keyword:keyword,
      page:0,
      list:[]
    })
    that.getList();
  },
  // 筛选
  sort_btn:function(e){
    var that = this;
    var index = e.currentTarget.dataset.index;
    that.setData({
      order:index,
      list:[],
      page:0
    })
    that.getList();
  },
  // 获取幻灯片
  getAdv:function(){
    var that = this;
    app.util.request({
      'url': 'entry/wxapp/index.adv',
      'cachetime': '30',
      data:{
        position:19
      },
      success: function (res) {
        console.log(res)
        var imageArr = res.data
        that.setData({
          imageArr: imageArr
        })
        console.log(that.data.imageArr)
      }
    })
  },
  saoyisao: function () {
    var that = this;
    wx.scanCode({ //扫描微信API
      success(res) { //成功
        console.log(res) //输出回调信息
        var code_num = res.result;
        app.util.request({
          'url': "entry/wxapp/index.goods_ids",
          data: {
            code_num: code_num
          },
          success: function (res) {
            if (res.data == '') {
              wx.showModal({
                title: '提示',
                content: '找不到该商品'
              })
            } else {
              wx.navigateTo({
                url: '/hyb_yl/mysubpages/pages/product/product?fid=&sid=' + res.data,
              })
            }
          }
        })
      },
      fail(res) {
        wx.showToast({
          title: '失败',
          icon: 'error',
          duration: 1000
        })
      },
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    that.setData({
      page:0,
      list:[]
    })
    that.getHospital_level();
    app.util.request({
      'url': 'entry/wxapp/index.base',
      success: function (res) {
        var baidukey = res.data.baidukey
        var bastitle = res.data.show_title
        wx.setStorageSync('bastitle', bastitle)
        wx.setStorageSync('baidukey', baidukey)
        that.setData({
          baseinfo: res.data,
        })
        wx.getLocation({
          type: 'wgs84', // 默认为 wgs84 返回 gps 坐标，gcj02 返回可用于 wx.openLocation 的坐标  
          success: function (res) {

            var longitude = res.longitude
            var latitude = res.latitude
            var baidukey = wx.getStorageSync('baidukey')
            wx.request({
              url: 'https://api.map.baidu.com/geocoder/v2/?ak=' + baidukey + '&location=' + latitude + ',' + longitude + '&output=json',
              data: {},
              header: {
                'Content-Type': 'application/json'
              },
              success: function (res) {
                console.log(res);
                if (res.data.message) {

                } else {
                  
                  var sematic_description = res.data.result.sematic_description
                  that.setData({
                    address:sematic_description
                  })
                  
                }
              },
              fail: function () { },
              complete: function () { }
            })
          },

          fail: function () {
            // fail  
          },
          complete: function () {
            // complete  
          }
        })

      },
      fail: function (err) {
        //console.log(err)
      },
    })

    that.getType();
    that.getList();
    that.getAdv();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.getList();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})